//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.member;

import com.aidong.dao.wills.ContractSignDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.WorkflowListDao;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberInfo;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"contractReset"})
public class ContractResetController extends BaseController {
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    MemberInfoDao memberInfoDao;
    @Resource
    WorkflowListDao workflowListDao;
    @Resource
    ContractSignDao contractSignDao;

    public ContractResetController() {
    }

    @RequestMapping({"query"})
    public ModelAndView query() {
        return new ModelAndView("member/contractreset/query");
    }

    @RequestMapping({"_query"})
    public ModelAndView _query(@RequestParam("contractCode") String contractCode) {
        ModelAndView mv = new ModelAndView("member/contractreset/_query");
        MemberContract contract = this.memberContractDao.getByContractCode(contractCode);
        if(contract == null) {
            mv.addObject("errMsg", "没有这个合同");
            return mv;
        } else {
            MemberInfo member = this.memberInfoDao.getByMemberCode(contract.getMemberCode());
            if(member == null) {
                member = new MemberInfo();
            }

            boolean apr = false;
            boolean prt = false;
            Date endTime = contract.getEndTime();
            if(contract.getNewEndTime() != null) {
                endTime = contract.getNewEndTime();
            }

            if(endTime != null && endTime.getTime() < System.currentTimeMillis()) {
                contract.setStatus(Integer.valueOf(3));
            }

            if(contract.getEffectiveDate() == null && contract.getStatus().intValue() != 3) {
                if(contract.getStatus().intValue() == 2 && !this.workflowListDao.chkWorkflowNotProc("04", contract.getContractCode(), (String[])null)) {
                    apr = true;
                }

                ContractSign sign = this.contractSignDao.getByContractCode(contract.getContractCode(), "01");
                if(sign == null) {
                    prt = true;
                }
            }

            mv.addObject("contract", contract);
            mv.addObject("member", member);
            if(apr) {
                mv.addObject("apr", Boolean.valueOf(true));
            }

            if(prt) {
                mv.addObject("prt", Boolean.valueOf(true));
            }

            return mv;
        }
    }

    @RequestMapping({"_addapprove"})
    @ResponseBody
    public String _addapprove(@RequestParam("contractCode") String contractCode) {
        MemberContract contract = this.memberContractDao.getByContractCode(contractCode);
        if(contract == null) {
            return this.getResult(false, "没有这个合同!");
        } else if(contract.getEffectiveDate() != null) {
            return this.getResult(false, "合同已终止!");
        } else {
            Date endTime = contract.getEndTime();
            if(contract.getNewEndTime() != null) {
                endTime = contract.getNewEndTime();
            }

            if(endTime != null && endTime.getTime() < System.currentTimeMillis()) {
                return this.getResult(false, "合同已过有效期!");
            } else if(contract.getStatus().intValue() != 2) {
                return this.getResult(false, "合同已生效,无须审批!");
            } else if(this.workflowListDao.chkWorkflowNotProc("04", contract.getContractCode(), (String[])null)) {
                return this.getResult(false, "合同已在审批中!");
            } else {
                this.memberContractDao.addContractApprove(contract);
                this.addUserLog("重置会籍合同特价审批", "合同号:" + contract.getContractCode());
                return this.getResult(true, "操作成功");
            }
        }
    }

    @RequestMapping({"_addprint"})
    @ResponseBody
    public String _addprint(@RequestParam("contractCode") String contractCode) {
        MemberContract contract = this.memberContractDao.getByContractCode(contractCode);
        if(contract == null) {
            return this.getResult(false, "没有这个合同!");
        } else if(contract.getEffectiveDate() != null) {
            return this.getResult(false, "合同已终止!");
        } else {
            Date endTime = contract.getEndTime();
            if(contract.getNewEndTime() != null) {
                endTime = contract.getNewEndTime();
            }

            if(endTime != null && endTime.getTime() < System.currentTimeMillis()) {
                return this.getResult(false, "合同已过有效期!");
            } else if(this.contractSignDao.getByContractCode(contract.getContractCode(), "01") != null) {
                return this.getResult(false, "合同可以正常签名打印!");
            } else {
                this.memberContractDao.addContractSign(contract);
                this.addUserLog("重置老会籍合同电子签名状态", "合同号:" + contract.getContractCode());
                return this.getResult(true, "操作成功");
            }
        }
    }
}
